package com.yunyv.iotudpserve.UDP.listener;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yunyv.iotudpserve.UDP.config.UdpConfig;
import com.yunyv.iotudpserve.common.Request;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.ip.udp.UnicastSendingMessageHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/********************************************************************************
 ** author： zhangtianhe
 ** date：2024年05月22日 15:11
 ** description&desc：
 *********************************************************************************/
@Slf4j
@RestController
@SaIgnore
@RequestMapping("/udp")
@Api(tags = "向嵌入式发送udp数据")
public class UdpClient {
    private final static Logger logger = LoggerFactory.getLogger(UdpClient.class);
    @SaIgnore
    @PostMapping("/send")
    public void send(@RequestBody @Validated Request<String> request) {

//        String message="#&HBS030001001/001/2;MOD:GT-TC03,0;CID:86635709230412213451;@5E79";
        String message = request.getData();
        UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler("localhost", UdpConfig.getSendingPort());
        logger.info("发送UDP信息: {" + message + "}");
        handler.handleMessage(MessageBuilder.withPayload(message).build());
        logger.info("发送成功");
    }

//    public static void main(String[] args) {
//        String message="#&HBS030001001/001/2;MOD:GT-TC03,0;CID:86635709230412213451;@5E79";
////        String message = request.getData();
//        UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler("localhost", UdpConfig.getSendingPort());
//        logger.info("发送UDP信息: {" + message + "}");
//        handler.handleMessage(MessageBuilder.withPayload(message).build());
//        logger.info("发送成功");
//    }
}
